package test.mq;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import hyl.base.mq.ISendReceive;
import hyl.base.mq.rabbit.MyRabbitMq;
import hyl.core.MyDate;
import hyl.core.info.CMsg;
public class testrabbitmq_send {
	private final static String QUEUE_NAME = "hello";
	public static void main(String[] args) throws java.io.IOException, TimeoutException, KeyManagementException,
			NoSuchAlgorithmException, URISyntaxException {
		test轮发();
		//test单发();
		//test广播();
	}
	public static void test单发() throws IOException {
		ISendReceive<CMsg> mrm = MyRabbitMq.getInstance(MyRabbitMq.M_一对一);// .set应答方式(false);
		for (int i = 0; i < 50; i++) {
			String s = "测试消息" + i;
			// mrm.sendgroup("jl911", "key911",s.getBytes());
			mrm.set主题( "语文").send("",s.getBytes());
		
			System.out.println(s + " 已经发送");
		}
	}
	public static void test轮发() throws IOException {
		ISendReceive<CMsg> mrm = MyRabbitMq.getInstance(MyRabbitMq.M_轮发);// .set应答方式(false);
		for (int i = 0; i < 50; i++) {
			String s = "测试消息" + i;
			// mrm.sendgroup("jl911", "key911",s.getBytes());
			mrm.set主题("分组", "英语").set回调函数(null,1).send("",s.getBytes());
			MyDate.sleep(1000);
			System.out.println(s + " 已经发送");
		}
	}
	public static void test广播() throws IOException {
		ISendReceive<CMsg> mrm = MyRabbitMq.getInstance(MyRabbitMq.M_群发);// .set应答方式(false);
		
		for (int i = 0; i < 50; i++) {
			String s = "测试消息" + i;
			// mrm.sendgroup("jl911", "key911",s.getBytes());
			mrm.set主题("分组", "").set回调函数( null,1).send("",s.getBytes());
		
			System.out.println(s + " 已经发送");
		}
	}
	public static void test群发() throws IOException {
		ISendReceive<CMsg> mrm = MyRabbitMq.getInstance(MyRabbitMq.M_群发);// .set应答方式(false);
		for (int i = 0; i < 50; i++) {
			String s = "测试消息" + i;
			// mrm.sendgroup("jl911", "key911",s.getBytes());
			mrm.set主题("分组", "").set回调函数( null,1).send("",s.getBytes());		
			System.out.println(s + " 已经发送");
		}
	}
	public static void testPublish()
			throws KeyManagementException, NoSuchAlgorithmException, URISyntaxException, IOException, TimeoutException {
		/**
		 * 创建连接连接到MabbitMQ
		 */
		ConnectionFactory factory = new ConnectionFactory();
		// 设置MabbitMQ所在主机ip或者主机名
		// factory.setHost("192.168.0.144");
		// factory.setPort(5672);
		// 原guest只能本地连接 ,远程连接必须再创建一个admin
		factory.setUri("amqp://admin:admin@192.168.0.144:5672");
		// 创建一个连接
		Connection connection = factory.newConnection();
		// 创建一个频道
		Channel channel = connection.createChannel();
		// 指定一个队列
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		// 发送的消息
		String message = "hello world!";
		// 声明exchange
		// channel.exchangeDeclare(EXCHANGE_NAME, "topic");
		// channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
		// 往队列中发出一条消息
		// exchange the exchange to publish the message to
		// routingKey the routing key
		// props other properties for the message - routing headers etc
		// body the message body
		for (int i = 0; i < 4; i++) {
			// channel.basicPublish(EXCHANGE_NAME, "routekey.1", null, message.getBytes());
			channel.basicPublish("", QUEUE_NAME, null, String.format(message + "%d", i).getBytes());
		}
		System.out.println(" [x] Sent '" + message + "'");
		// 关闭频道和连接
		channel.close();
		connection.close();
	}
}
